﻿using Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Scheduledscheduling
{
    public static class HttpReuestUtility
    {
        /// <summary>
        /// List转换为DataTable
        /// </summary>
        /// <typeparam name="T">列表项类型</typeparam>
        /// <param name="list">要转换的集合</param>
        /// <returns></returns>
        public static DataTable List2DataTable<T>(this List<T> list) where T : class
        {
            var dt = new DataTable();
            var ps = typeof(T).GetProperties();

            var columns = ps.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray();
            dt.Columns.AddRange(columns);

            foreach (T t in list)
            {
                DataRow dr = dt.NewRow();
                foreach (PropertyInfo pi in ps)
                {
                    if (dt.Columns.Contains(pi.Name) && pi.GetValue(t) != null)
                    {
                        dr[pi.Name] = pi.GetValue(t);
                    }
                }
                dt.Rows.Add(dr);
            }

            return dt;
        }

        /// <summary>
        /// 发送jason到服务器
        /// </summary>
        /// <param name="SendDate"></param>
        /// <param name="InterfaceName"></param>
        /// <returns></returns>
        public static string GetResponseData(string SendDate, string InterfaceName)
        {

            string reJson = string.Empty;
            try
            {
                if (SendDate == "")
                {
                    SendDate = "{}";
                }
                byte[] bytes = Encoding.UTF8.GetBytes(SendDate);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(InterfaceName);
                request.Method = "POST";
                //request.ContentType = "application/x-www-form-urlencoded";
                //request.Accept = "text/plain";
                request.ContentType = "application/json";
                request.Accept = "application/json";
                request.ContentLength = bytes.Length;
                Stream reqstream = request.GetRequestStream();
                reqstream.Write(bytes, 0, bytes.Length);

                //声明一个HttpWebRequest请求  
                request.Timeout = 60000;
                //设置连接超时时间  
                request.Headers.Set("Pragma", "no-cache");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream streamReceive = response.GetResponseStream();
                Encoding encoding = Encoding.UTF8;

                StreamReader streamReader = new StreamReader(streamReceive, encoding);
                string strResult = streamReader.ReadToEnd();
                streamReceive.Dispose();
                streamReader.Dispose();
                reJson = strResult;
                return reJson;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }




        /// <summary>
        /// 发送jason到服务器
        /// </summary>
        /// <param name="SendDate"></param>
        /// <param name="InterfaceName"></param>
        /// <returns></returns>
        public static string GetResponseDataByGet(string InterfaceName)
        {
            string reJson = string.Empty;
            try
            {

    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(InterfaceName);
                request.Method = "GET";

                //声明一个HttpWebRequest请求  
                request.Timeout = 60000;
                //设置连接超时时间  
                request.Headers.Set("Pragma", "no-cache");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream streamReceive = response.GetResponseStream();
                Encoding encoding = Encoding.UTF8;

                StreamReader streamReader = new StreamReader(streamReceive, encoding);
                string strResult = streamReader.ReadToEnd();
                streamReceive.Dispose();
                streamReader.Dispose();
                reJson = strResult;
                return reJson;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public static string GetResponseDataByPost(TaskOptions op)
        {
            string reJson = string.Empty;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(op.ApiUrl);
                request.Method = "POST";
                request.ContentType = "application/json";
                request.Accept = "application/json";
                request.Timeout = 60000;
                request.Headers.Set("Pragma", "no-cache");
                if (!string.IsNullOrEmpty(op.AuthKey))
                {
                    request.Headers.Add("Authorization", $"Bearer {op.AuthKey}");
                }
                string jsonData = op.AuthValue == null ? "" : JsonConvert.SerializeObject(op.AuthValue);
                byte[] bytes = string.IsNullOrEmpty(jsonData) ? new byte[0] : Encoding.UTF8.GetBytes(jsonData);
                request.ContentLength = bytes.Length;
                using (Stream reqStream = request.GetRequestStream())
                {
                    if (bytes.Length > 0)
                    {
                        reqStream.Write(bytes, 0, bytes.Length);
                    }
                }
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (Stream streamReceive = response.GetResponseStream())
                using (StreamReader streamReader = new StreamReader(streamReceive, Encoding.UTF8))
                {
                    reJson = streamReader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
               throw ex;
            }
            return reJson;
        }
    }
}
